package cn.ondraw.ragflow.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.common.core.domain.R;
import cn.ondraw.ragflow.pojo.request.CreateDatasetRequest;
import cn.ondraw.ragflow.pojo.request.DeleteDatasetsRequest;
import cn.ondraw.ragflow.pojo.request.ListDatasetsRequest;
import cn.ondraw.ragflow.pojo.request.UpdateDatasetRequest;
import cn.ondraw.ragflow.service.DatasetService;
import org.springframework.web.bind.annotation.*;

/**
 * 知识库
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/v1/datasets")
public class DataSetController {

    private final DatasetService datasetService;

    /**
     * 创建数据集
     *
     * @param request 创建数据集的请求对象
     * @return 创建数据集的响应对象
     */
    @PostMapping
    public R<Object> createDataset(@RequestBody CreateDatasetRequest request) {
        try {
            return R.ok(datasetService.createDataset(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除数据集
     *
     * @param request 删除数据集的请求对象
     * @return 删除数据集的响应对象
     */
    @DeleteMapping()
    public R<Object> deleteDatasets(@RequestBody DeleteDatasetsRequest request) {
        try {
            return R.ok(datasetService.deleteDatasets(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新数据集配置
     *
     * @param datasetId 数据集 ID
     * @param request   更新数据集的请求对象
     * @return 更新数据集的响应对象
     */
    @PutMapping("/{datasetId}")
    public R<Object> updateDataset(@PathVariable String datasetId, @RequestBody UpdateDatasetRequest request) {
        try {
            return R.ok(datasetService.updateDataset(datasetId, request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 列出数据集
     *
     * @param request 列出数据集请求对象
     * @return 响应实体
     */
    @GetMapping
    public R<Object> listDatasets(ListDatasetsRequest request) {
        try {
            return R.ok(datasetService.listDatasets(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}
